//
//  BooleanButtonLabelView.m
//  YiJuZhangShou2.0
//
//  Created by URANUS on 16/3/24.
//  Copyright © 2016年 Mr.X. All rights reserved.
//

#import "BooleanButtonLabelView.h"
#import "Masonry.h"

@interface BooleanButtonLabelView()
@property (nonatomic, strong) UILabel *titleLabel;
@end

@implementation BooleanButtonLabelView
#pragma mark Initialize
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self loadBooleanButtonLabelView];
    }
    return self;
}

#pragma mark - Event Response
- (void)selectClick:(DrawBooleanButtonView *)sender{
    
    if (self.delegate != nil && [self.delegate respondsToSelector:@selector(selectBooleanButtonWithState: andButton:)]) {
        [self.delegate selectBooleanButtonWithState:sender.isSelect andButton:sender];
    }
}


- (void)selectButtonAction{
    [self.drawBooleanButtonView setIsSelect:!self.drawBooleanButtonView.isSelect];
    if (self.delegate != nil && [self.delegate respondsToSelector:@selector(selectBooleanButtonWithState: andButton:)]) {
        [self.delegate selectBooleanButtonWithState:self.drawBooleanButtonView.isSelect andButton:self.drawBooleanButtonView];
    }
}

#pragma mark private
- (void)loadBooleanButtonLabelView{
    [self.drawBooleanButtonView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self);
        make.centerY.equalTo(self);
        make.height.equalTo(self).multipliedBy(0.5);
        make.width.equalTo(self.drawBooleanButtonView.mas_height);
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.drawBooleanButtonView.mas_right).offset(5);
        make.top.bottom.right.equalTo(self);
    }];
}

#pragma mark - Getter

- (DrawBooleanButtonView *)drawBooleanButtonView{
   
    if (_drawBooleanButtonView == nil) {
        _drawBooleanButtonView = [DrawBooleanButtonView new];
        [_drawBooleanButtonView addTarget:self action:@selector(selectClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_drawBooleanButtonView];
    }
    return _drawBooleanButtonView;
}

-(UILabel *)titleLabel{
 
    if (_titleLabel == nil) {
        _titleLabel = [UILabel new];
        _titleLabel.font = TextFont14;
        _titleLabel.textColor = TextColor80;
        _titleLabel.userInteractionEnabled = YES;
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectButtonAction)];
        [_titleLabel addGestureRecognizer:tapGesture];
        [self addSubview:_titleLabel];
    }
    return _titleLabel;
}

#pragma mark - Setter
-(void)setModel:(BooleanButtonLabelModel *)model{
    
    if (model.title != nil) {
        self.titleLabel.text = model.title;
    }
    
    if (model.pickBorderColor != nil) {
        self.drawBooleanButtonView.pickBorderColor = model.pickBorderColor;
    }
    
    if (model.pickStrokeColor != nil) {
        self.drawBooleanButtonView.pickStrokeColor = model.pickStrokeColor;
    }
    
    if (model.pickBackgoundColor != nil) {
        self.drawBooleanButtonView.pickBackgoundColor = model.pickBackgoundColor;
    }
    
    if (model.blankBorderColor != nil) {
        self.drawBooleanButtonView.blankBorderColor = model.blankBorderColor;
    }
    
    if (model.blankBackgoundColor != nil) {
        self.drawBooleanButtonView.blankBackgoundColor = model.blankBackgoundColor;
    }
    
    if (model.font != nil) {
        self.titleLabel.font = model.font;
    }
    
    if (model.textColor != nil) {
        self.titleLabel.textColor = model.textColor;
    }
}

-(void)setIsSelect:(Boolean)isSelect{
    self.drawBooleanButtonView.isSelect = isSelect;
}

@end
